//
//  MoreView.m
//  B段项目
//
//  Created by 昶博 on 15/9/29.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "MoreView.h"
#import "PrefixHeader.pch"
#import "MoreCollectionViewCell.h"
#import "ForthModel.h"
#import "AFNetWorkingW.h"
#import "NextViewController.h"
@interface MoreView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic ,strong)NSArray *arr;

@end
@implementation MoreView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     
        
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
        flowlayout.minimumInteritemSpacing = 10*w;
        flowlayout.minimumLineSpacing = 10*h;
        flowlayout.itemSize = CGSizeMake((self.frame.size.width/4+20*w), self.frame.size.height/5);
        flowlayout.sectionInset = UIEdgeInsetsMake(10*h, 5*w, 5*h, 5*w);
        self.collcetionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowlayout];
        self.collcetionView.delegate = self;
        self.collcetionView.dataSource = self;
        [self.collcetionView registerClass:[MoreCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        self.collcetionView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.collcetionView];

        [self dataHandle];
        
        
    }
    return self;
    
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.arr.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
     
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    ForthModel*model = [[ForthModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    cell.Model =model;
    
    return cell;
    
    
}
-(void)dataHandle
{
    self.arr = [NSArray array];
    
  NSString *urlString = @"http://223.99.255.20/app.api.autohome.com.cn/autov4.9.5/News/newsmyradiolist-pm1-tp0.json";
    
    [AFNetWorkingW  netWorkingHanderGetGataWithUrl:urlString resultBlock:^(id result) {
     
        self.arr = result[@"result"][@"list"];
        
        [self.collcetionView reloadData];
        
    }];
    
}
// 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    ForthModel *model = [[ForthModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
       NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"pushnext" object:model];
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
